//
//  STupianViewController.m
//  News
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 PEAK. All rights reserved.
//

#import "STupianViewController.h"

@interface STupianViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic ,retain)UITableView *tableView;
@property(nonatomic, retain)NSArray *arr;
@property(nonatomic, retain)NSString*desc;
@property(nonatomic,retain)NSMutableArray *PicArr;
@property(nonatomic ,retain)NSMutableArray* pisArr;
@property(nonatomic, retain)SPic *pic;
@property(nonatomic, retain)NSString *str;
@property(nonatomic, retain) UIActivityIndicatorView *activityIndicator;
@property(nonatomic ,retain)UIImageView *imageView;

@end

@implementation STupianViewController
- (void)dealloc
{
    [_str release];
    [_desc release];
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title  =@"图片";
    self.pisArr = [NSMutableArray array];
    self.PicArr = [NSMutableArray array];

    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui"] style:UIBarButtonItemStyleDone target:self action:@selector(fanHui)];
     [self create];
    // 请求出现小菊花
    self.imageView  = [[UIImageView alloc] initWithFrame:self.view.bounds];
    _imageView.image = [UIImage imageNamed:@"5EA2D64D-15BE-43D8-AB18-E15BCE039C8F"];
    _imageView.alpha =1;
    [self.view addSubview:_imageView];
    
    _activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    [_activityIndicator setCenter:_imageView.center];
    _activityIndicator.backgroundColor= [UIColor grayColor];
    [_activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [_imageView addSubview:_activityIndicator];
    
    [_activityIndicator startAnimating];
    
   
    self.isUpLoading = NO;
    [self addHeader];
    [self addFooter];
   
    
}
// 下拉刷新
-(void)addHeader
{
    __block STupianViewController *pic = self;
    [self.tableView addHeaderWithCallback:^{
        pic.isUpLoading = NO;// 标记为下拉
        [pic getData];// 重新请求数据
    
    }];
    // 自动刷新
    [self.tableView headerBeginRefreshing];
}

// 上拉加载更多
-(void)addFooter
{
    __block STupianViewController *pic = self;
    // 尾部控件
    [self.tableView addFooterWithCallback:^{
        // 调用block
        pic.isUpLoading = YES;
        [pic getDataOne];
    }];
}

-(void)fanHui
{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}

-(void)getDataOne
{
    


    NSString *str1 = [NSString stringWithFormat:@"http://c.m.163.com/photo/api/morelist/0096/54GI0096/%@.json",_str];
    
    
    [SNetWorkHandle getDataWithURL: str1 completion:^(id result) {
        
        if (self.isUpLoading == NO) {
            [self.pisArr removeAllObjects];
        }
        
        for (NSDictionary *temp in result) {
            
           
            
            SPic * pic = [[SPic alloc] initWithDic:temp];
            if (pic.pics.count != 0) {
                
                 [_pisArr addObject:pic];
                
            }
     
          
            
            
            
        }
        self.str = [[_pisArr lastObject] setid];

        [self.tableView footerEndRefreshing];
        [_tableView reloadData];
        
    }];
    
}


-(void)getData
{
    
    [SNetWorkHandle getDataWithURL:@"http://c.3g.163.com/photo/api/list/0096/54GI0096.json" completion:^(id result) {

        if (self.isUpLoading == NO) {
            [self.pisArr removeAllObjects];
        }
        
        for (NSDictionary *temp in result) {
            SPic * pic = [[SPic alloc] initWithDic:temp];
            if (pic.pics.count != 0) {
                [_pisArr addObject:pic];
            }
            
         
        }
        
  self.str = [[_pisArr lastObject] setid];
        [self.tableView headerEndRefreshing];
        
         [_tableView reloadData];
        [_imageView removeFromSuperview];
    }];
    
}
// 创建
-(void)create
{
    self.tableView  = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = [UIScreen mainScreen].bounds.size.height / 2.3;
    [self.view addSubview:_tableView];
    [_tableView release];
    
    
    [_tableView registerClass:[SPic1TableViewCell class] forCellReuseIdentifier:@"a"];
    
    [ _tableView registerClass:[SPic4TableViewCell class] forCellReuseIdentifier:@"b"];
   [ _tableView registerClass:[SPic3TableViewCell class] forCellReuseIdentifier:@"c"];

    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return _pisArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    
//    NSLog(@"%@", _pisArr);
    // 判断 取余显示的是第几张照片
        if (indexPath.row % 3 == 0) {
            
            
            SPic1TableViewCell * cell = [[SPic1TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"a"];
            
            cell.Pic = _pisArr[indexPath.row];
            return cell;
            
            
        }else if
            (indexPath .row % 3 == 1)
        {
      
            SPic4TableViewCell * cell = [[SPic4TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"b"];
            
            cell.Pic = _pisArr[indexPath.row];
            return cell;
        }
        else if (indexPath .row %3 == 2)
        {
            SPic3TableViewCell * cell = [[SPic3TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"c"];
            
            cell.Pic = _pisArr[indexPath.row];
            
            return cell;
            
        }
    
  
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SPicDetails1ViewController *pic = [[SPicDetails1ViewController alloc] init];
    
    
    SPic * temp = [_pisArr objectAtIndex:indexPath.row];
    pic.str = temp.setid;
    
    [self.navigationController pushViewController:pic animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
